package org.jetlinks.demo.protocol.siterwell.tcp.message;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.jetlinks.demo.protocol.siterwell.tcp.constant.DeviceType;
import org.jetlinks.demo.protocol.siterwell.tcp.util.ByteUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 读取传感器数据响应数据
 * 首先平台主动下发读取网关传感器指令，网关接收到指令后，上传传感器数据
 * 网关发往平台的消息数据
 */
@Getter
@Setter
@NoArgsConstructor
public class D2SReadSensorReply extends AbstractCommandRequest {

    private int totalDataSegment;
    private List<SensorData> sensorDataList;

    public static D2SReadSensorReply of(byte[] data) {
        D2SReadSensorReply message = new D2SReadSensorReply();
        message.fromBytes(data, 0);
        return message;
    }


    @Override
    protected void parseData() {
        String hexDataSource = this.getHexDataSource();
        String hexTotalDataSegment = ByteUtils.subHexStrByByte(hexDataSource, 0, 1);
        this.totalDataSegment = ByteUtils.hexToInt(hexTotalDataSegment);
        this.sensorDataList = new ArrayList<>();
        int startByte = 1;
        for (int i = 0; i < totalDataSegment; i++) {
            String curDataSegment = ByteUtils.subHexStrByByte(hexDataSource, startByte, startByte + 113);
            List<SensorData> curSegmentSensorList = parseSensorDataList(curDataSegment);
            sensorDataList.addAll(curSegmentSensorList);
            startByte += 113;
        }

    }

    private List<SensorData> parseSensorDataList(String sensorDataSegment) {
        List<SensorData> sensorDataList = new ArrayList<>();
        int startByte = 1, len = 16;
        for (int i = 0; i < len; i++) {
            String sensorData = ByteUtils.subHexStrByByte(sensorDataSegment, startByte, startByte + 7);
            Optional<SensorData> optionalSensorData = parseSensorData(sensorData);
            optionalSensorData.ifPresent((sensor) -> {
                sensorDataList.add(sensor);
            });
            startByte += 7;
        }
        return sensorDataList;
    }

    private Optional<SensorData> parseSensorData(String sensorData) {
        boolean isEmptyData = sensorData.matches("f{14}");
        if (isEmptyData || sensorData.length() != 14) {
            return Optional.empty();
        }
        String type = ByteUtils.subHexStrByByte(sensorData, 0, 1);
        String childDeviceId = ByteUtils.subHexStrByByte(sensorData, 1, 5);
        String roomNo = ByteUtils.subHexStrByByte(sensorData, 5, 7);
        Optional<DeviceType> optionalDeviceType = DeviceType.of(type);
        if (optionalDeviceType.isPresent()) {
            DeviceType deviceType = optionalDeviceType.get();
            SensorData sensor = new SensorData(deviceType.getCode(), deviceType.getDesc(), childDeviceId, roomNo);
            return Optional.of(sensor);
        }
        return Optional.empty();
    }

    @Getter
    @Setter
    @AllArgsConstructor
    static class SensorData {
        private String type;
        private String name;
        private String childDeviceId;
        private String roomNo;

    }
}
